package com.fone.player.storage;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.fone.player.entity.CustomChannel;
import com.fone.player.util.L;

/**
 * 
 * 自定义频道表适配器
 * 
 * @author wangyang
 * @since 2014年4月29日
 */
class CustomChannelDataBaseAdapter implements IFoneDatabase {
	private static final String TAG = CustomChannelDataBaseAdapter.class
			.getSimpleName();
	private static final CustomChannelDataBaseAdapter mInstance = new CustomChannelDataBaseAdapter();
	private OperateDataBaseTemplate mGeneralDataBaseTemplate = OperateDataBaseTemplate
			.getInstance();

	private CustomChannelDataBaseAdapter() {

	}

	static CustomChannelDataBaseAdapter getInstance() {
		return mInstance;
	}

	/**
	 * 
	 * 添加自定义频道
	 * 
	 * @param customChannelList
	 *            自定义频道集合
	 * @return int 操作结果<br>
	 *         1插入成功<br>
	 *         -1 插入异常
	 * 
	 */
	public int addCustomChannelList(List<CustomChannel> customChannelList) {
		synchronized (TAG) {
			int id = -1;
			try {
				mGeneralDataBaseTemplate.open();

				// 清空表
				mGeneralDataBaseTemplate.delete(TB_CUSTOM_CHANNEL, null, null);

				// 添加
				long newId = 0;
				for (CustomChannel customChannel : customChannelList) {
					ContentValues contentValues = new ContentValues();
					contentValues.put(CUSTOM_CHANNEL_ID,
							customChannel.getCustomChannelId());
					contentValues.put(CUSTOM_CHANNEL_NAME,
							customChannel.getCustomChannelName());
					contentValues.put(CUSTOM_CHANNEL_TYPE,
							customChannel.getCustomChannelType());
					contentValues.put(CUSTOM_CHANNEL_URL,
							customChannel.getCustomChannelUrl());
					contentValues.put(CUSTOM_CHANNEL_IS_CUSTOM,
							customChannel.getCustomChannelIsCustom());
					contentValues.put(CUSTOM_CHANNEL_IS_SHOW,
							customChannel.getCustomChannelIsShow());
					contentValues.put(CUSTOM_CHANNEL_IS_NEW,
							customChannel.getCustomChannelIsNew());
					newId = mGeneralDataBaseTemplate.insert(TB_CUSTOM_CHANNEL,
							null, contentValues);

				}

				id = newId > 0 ? 1 : -1;
			} catch (Exception e) {
				e.printStackTrace();
				L.v(TAG, e.getMessage());
				return -1;
			} finally {

				mGeneralDataBaseTemplate.close();
			}
			return id;
		}
	}

	/**
	 * 
	 * 获得所有自定义频道列表
	 * 
	 * @return List<CustomChannel> 自定义频道集合
	 */
	public List<CustomChannel> getCustomChannelList() {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_CUSTOM_CHANNEL);

		Cursor cursor = null;
		List<CustomChannel> customChannelList = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}

			customChannelList = new ArrayList<CustomChannel>();
			// 读取数据
			while (cursor.moveToNext()) {
				CustomChannel customChannel = new CustomChannel();
				customChannel.setId(cursor.getInt(cursor
						.getColumnIndex(_ID)));
				customChannel.setCustomChannelId(cursor.getString(cursor
						.getColumnIndex(CUSTOM_CHANNEL_ID)));
				customChannel.setCustomChannelName(cursor.getString(cursor
						.getColumnIndex(CUSTOM_CHANNEL_NAME)));
				customChannel.setCustomChannelType(cursor.getString(cursor
						.getColumnIndex(CUSTOM_CHANNEL_TYPE)));
				customChannel.setCustomChannelUrl(cursor.getString(cursor
						.getColumnIndex(CUSTOM_CHANNEL_URL)));
				customChannel.setCustomChannelIsCustom(cursor.getString(cursor
						.getColumnIndex(CUSTOM_CHANNEL_IS_CUSTOM)));
				customChannel.setCustomChannelIsShow(cursor.getString(cursor
						.getColumnIndex(CUSTOM_CHANNEL_IS_SHOW)));
				customChannel.setCustomChannelIsNew(cursor.getString(cursor
						.getColumnIndex(CUSTOM_CHANNEL_IS_NEW)));
				customChannelList.add(customChannel);
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {

				cursor.close();

			}
			mGeneralDataBaseTemplate.close();

		}
		return customChannelList;
	}

}
